package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-14
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    @Override
    public void saveSubject(MultipartFile multipartFile, EduSubjectService service) {
        try{

            InputStream in=multipartFile.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(service)).sheet().doRead();


        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //11查询1级分类 parent_id=0
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        //帮我们注入了，不需要掉mapper
        List<EduSubject> oneList = baseMapper.selectList(wrapper);

        //22查询2级分类 parent_id!=0
        QueryWrapper<EduSubject> wrapper2=new QueryWrapper<>();
        wrapper2.ne("parent_id","0");
        List<EduSubject> TwoList = baseMapper.selectList(wrapper2);


        List<OneSubject> finalSubject=new ArrayList<>();
        //33封装1级分类
        //查询出来的一级分类list遍历，得到每个一级分类对象
        for (int i = 0; i < oneList.size(); i++) {
            EduSubject eduSubject = oneList.get(i);
            //把eduSubject的值获取出来，放到oneSubject里面
            System.out.println(eduSubject);
            OneSubject oneSubject = new OneSubject();
            oneSubject.setId(eduSubject.getId());
            oneSubject.setTitle(eduSubject.getTitle());
            //简化版如下
//            BeanUtils.copyProperties(eduSubject,oneSubject);

            //在一级分类里面遍历查询所以二级分类
            //创建list集合，封装每个一级分类的二级子集
            List<TwoSubject> twoFianalSub=new ArrayList<>();
            for (int m = 0; m < TwoList.size(); m++) {
                //获取每个二级分类
                EduSubject eduSubject1 = TwoList.get(m);
                //判断二级分类的ParentID=一级分类的id，则为其子集
                if (eduSubject1.getParentId().equals(oneSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    //把eduSubject1里的所有值，复制到twoSubject
                    BeanUtils.copyProperties(eduSubject1,twoSubject);
                    twoFianalSub.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFianalSub);
            finalSubject.add(oneSubject);
        }

        return finalSubject;
    }
}
